package com.kehao.account_book.repository;

import com.kehao.account_book.pojo.Expenditure;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author kehao
 * @date 2020/2/22-14:55
 */
public interface ExpenditureRepository extends JpaRepository<Expenditure,Integer> {
    List<Expenditure> findByUsernameAndYearAndMonthAndDayAndHourAndMinAndSecond(
            String username,Integer year,Integer month,Integer day,Integer hour,Integer min,Integer Second);

    @Modifying
    @Query("UPDATE Expenditure e SET e.type = ?1, e.money = ?2 ,e.comment = ?3,e.modify =?4 WHERE e.id = ?5")
    void updateExpenditureById(int type,double money,String comment,int modify,int id);//通过id更新expenditure

    //找出某年某月比客户端的上次更新时间更新的，用于更新
    List<Expenditure> findByModifyGreaterThanAndUsernameAndYearAndMonth(int modify,String username,int year,int month);

    @Query("SELECT max(e.modify) from Expenditure e where e.username = ?1")//获取最大的modify值，用于返回
    long getMaxModify(String username);

    @Query("SELECT count(e.id) from Expenditure e where e.username = ?1")//获取该用户的交易量，用于返回
    long getCountOfTable(String username);


}
